/**
 * Created by xiaoz on 16/8/13.
 */
define(["utils","base"],function(utils,baseClass){
    var Component = function(config){
        var _this = this;
        Component.baseConstructor.call(this,config);
        var className = "yy-paginator";
        this.$el.addClass(className);
        this.firstSectionCount = this.config.firstSectionCount || 3;
        this.secondSectionPreCount = this.config.secondSectionPreCount || 2;
        this.secondSectionNextCount = this.config.secondSectionNextCount ||2;
        this.thirdSectionCount = this.config.thirdSectionCount ||3;

        var changeName = this.config.comKey+"_change";
        var changeNameMethod;
        if(this.pageview.plugin&&this.pageview.plugin[changeName]){
           changeNameMethod = this.pageview.plugin[changeName];
        }

        this.$el.bind("click",function(e){
          if(_this.inLoading){
            return;
          }
          var target = e.target;
          var pageIndex = target.getAttribute("data-pageindex");
          if(pageIndex){
            _this.inLoading = true;
            target.innerHTML = "&nbsp;";
            $(target).spin('tiny', '#999');
            _this.changeFunc && _this.changeFunc(pageIndex);
            changeNameMethod && changeNameMethod.call(_this.pageview.plugin,_this,{pageIndex:pageIndex});
          }
        });

        this.$el.attr('unselectable','on').bind('selectstart', function(){ return false; });
    }

    utils.extendClass(Component,baseClass);

    Component.prototype.bindChange = function(cb){
      this.changeFunc = cb;
    };
    Component.prototype._getSectionInfo=function (curPageIndex,pageCount) {
      var sectionInfo = {
          "firstSectionStart": 0,
          "firstSectionEnd": 0,
          "secondSectionStart": 0,
          "secondSectionEnd": 0,
          "thirdSectionStart": 0,
          "thirdSectionEnd": 0,
          "isShowFirstPoint": false,
          "isShowSecondPoint": false
      };
      /*计算第二段的起始Index*/
      sectionInfo.secondSectionStart = curPageIndex - this.secondSectionPreCount;
      /*计算第二段的结束Index*/
      sectionInfo.secondSectionEnd = curPageIndex + this.secondSectionNextCount;
      /*对第二段的起始和结束值 进行校验 来决定正确的起始和结束值
      并且确定是否创建第一段 和 第二段
      */
      if (sectionInfo.secondSectionStart <= 1) {
          sectionInfo.secondSectionStart = 1;
          /*不创建第一段*/
          sectionInfo.firstSectionStart = 0;
          sectionInfo.firstSectionEnd = 0;
      }
      else {
          sectionInfo.firstSectionStart = 1;
          sectionInfo.firstSectionEnd = this.firstSectionCount;
          /*进行第一段结束Index进行校验 确定正确的第一段的结束值 以及是否创建第一段和第二段之间的省略号 */
          if (sectionInfo.firstSectionEnd >= sectionInfo.secondSectionStart - 1) {
              sectionInfo.isShowFirstPoint = false;
              sectionInfo.firstSectionEnd = sectionInfo.secondSectionStart - 1;
          }
          else {
              sectionInfo.isShowFirstPoint = true;
          }
      }

      if (sectionInfo.secondSectionEnd >= pageCount) {
          sectionInfo.secondSectionEnd = pageCount;
          /*不创建第三段*/
          sectionInfo.thirdSectionStart = 0;
          sectionInfo.thirdSectionEnd = 0;
      } else {
          sectionInfo.thirdSectionEnd = pageCount;
          sectionInfo.thirdSectionStart = pageCount - this.thirdSectionCount + 1;
          /*进行第三段起始Index 的正确性 校验 以及确定是否创建 第二段和第三段之间的省略号*/
          if (sectionInfo.thirdSectionStart > sectionInfo.secondSectionEnd + 1) {
              sectionInfo.isShowSecondPoint = true;
          }
          else {
              sectionInfo.thirdSectionStart = sectionInfo.secondSectionEnd + 1;
              sectionInfo.isShowSecondPoint = false;
          }

      }
      return sectionInfo;
  }

  Component.prototype._createNumBlock = function(pageIndex,pageBlock){
    var c = pageIndex == pageBlock ? "yy-paginator-num-selected" : "yy-paginator-num";
    return $("<span class='" + c + "' data-pageindex='" + pageBlock + "'>" + pageBlock + "</span>");
  };

    Component.prototype.init = function(pageIndex,pageCount){
      this.inLoading = false;
      var sectionInfo = this._getSectionInfo(pageIndex,pageCount);
      //console.log(sectionInfo);
      this.$el.empty();
      if(sectionInfo.firstSectionStart>0){
        for(var i=sectionInfo.firstSectionStart;i<=sectionInfo.firstSectionEnd;i++){
          var numBlock = this._createNumBlock(pageIndex,i);
          this.$el.append(numBlock);
        }
      }

      if(sectionInfo.isShowFirstPoint){
        this.$el.append("<span class='yy-paginator-point'>...</span>");
      }


      if(sectionInfo.secondSectionStart>0){
        for(var i=sectionInfo.secondSectionStart;i<=sectionInfo.secondSectionEnd;i++){
          var numBlock = this._createNumBlock(pageIndex,i);
          this.$el.append(numBlock);
        }
      }

      if(sectionInfo.isShowSecondPoint){
        this.$el.append("<span class='yy-paginator-point'>...</span>");
      }
      if(sectionInfo.thirdSectionStart>0){
        for(var i=sectionInfo.thirdSectionStart;i<=sectionInfo.thirdSectionEnd;i++){
          var numBlock = this._createNumBlock(pageIndex,i);
          this.$el.append(numBlock);
        }
      }


    };
    Component.prototype.go = function(pageIndex,pageCount){
        var _this= this;
      if(isNaN(pageIndex)){
        console.error("paginator go方法传参必须都是数字");
      }
      if(isNaN(pageCount)){
        pageCount = 1;
      }
      pageIndex = parseInt(pageIndex);
      pageCount = parseInt(pageCount);
      pageCount = pageCount<=0?1:pageCount;
      if(pageIndex===1){
        this.pageCount = pageCount||this.pageCount||1;
        this.init(1,pageCount);
      }else{
        this.init(pageIndex,this.pageCount);
      }
      if (pageCount <= 1) {
          this.$el.hide();
      }else{
          window.setTimeout(function(){
              _this.$el.show();
          },200);
      }

    };

    return Component;
});
